//
//  TCLYEditorTieZiViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/1.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYEditorTieZiViewController.h"
#import "TCLYCommunityView.h"
#import "Masonry.h"
#import "Macros.h"
#import "UIView+HB.h"
#import "TCLYMineModel.h"
#import "TCLYUser.h"

@interface TCLYEditorTieZiViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewLayout;
@property (nonatomic, strong) TCLYCommunityView * communtyView;
@property (nonatomic, strong) TCLYMineModel * mineModel;
@property (nonatomic, strong) TCLYUser * user;
@end

@implementation TCLYEditorTieZiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.mineModel = [[TCLYMineModel alloc] init];
    self.user = [[TCLYUser alloc] init];
    [self createView];
    [self createTitleView];
}
-(void) createView{
    
    self.communtyView = [[TCLYCommunityView alloc] init];
    self.communtyView.titleTextView.delegate = self;
    self.communtyView.contentTextView.delegate = self;
    self.communtyView.titleTextView.text = self.editorTieZiDic[@"notename"];
    self.communtyView.contentTextView.text = self.editorTieZiDic[@"describle"];
    
    UITapGestureRecognizer * textFeildTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:textFeildTap];
    
    
    [self.bgView addSubview:self.communtyView];
    [self.communtyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0).offset(12);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(200);
    }];
    
}
-(void)createTitleView{
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"修改帖子";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.tag = 92;
    [rightBtn setImage:[UIImage imageNamed:@"send"] forState:UIControlStateNormal];
    
    
    rightBtn.frame = CGRectMake(screenWidth-12-20, 22, 30, 20);
    [rightBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *)sender).tag) {
        case 91:{
            [self.navigationController popViewControllerAnimated:YES];
            self.navigationController.navigationBarHidden = NO;
            self.tabBarController.tabBar.hidden = YES;
        }
            break;
        case 92:{
            NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId, @"uid", self.editorTieZiDic[@"nid"],@"nid",self.communtyView.titleTextView.text,@"notename", self.communtyView.contentTextView.text, @"describle", nil];
            
            NSLog(@"aaa%@",self.communtyView.titleTextView.text);
            NSLog(@"ccc%@",self.communtyView.contentTextView.text);
            NSLog(@"%@",self.editorTieZiDic[@"nid"]);
            
            
            [self.mineModel editorTieZiParameters:dic];
            
            self.navigationController.navigationBarHidden = YES;
            self.tabBarController.tabBar.hidden = NO;
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
            break;
    }
}
- (void)textViewDidChange:(UITextView *)textView{
    if ([textView isEqual:self.communtyView.titleView]) {
        //计算文本的高度
        //获得textView的初始尺寸
        CGFloat width = CGRectGetWidth(textView.frame);
        CGFloat height = CGRectGetHeight(textView.frame);
        CGSize newSize = [textView sizeThatFits:CGSizeMake(width,MAXFLOAT)];
        CGRect newFrame = textView.frame;
        newFrame.size = CGSizeMake(fmax(width, newSize.width), fmax(height, newSize.height));
        self.communtyView.titleView.height = newFrame.size.height;
        
    }else{
        //计算文本的高度
        //获得textView的初始尺寸
        CGFloat width = CGRectGetWidth(textView.frame);
        CGFloat height = CGRectGetHeight(textView.frame);
        CGSize newSize = [textView sizeThatFits:CGSizeMake(width,MAXFLOAT)];
        CGRect newFrame = textView.frame;
        newFrame.size = CGSizeMake(fmax(width, newSize.width), fmax(height, newSize.height));
        self.communtyView.conteneView.height = newFrame.size.height;
        
    }
    self.communtyView.height = self.communtyView.conteneView.height + self.communtyView.titleView.height + 10+24;
    if (self.communtyView.height <= screenHeight-64-44) {
        self.scrollViewLayout.constant = screenHeight-64-44;
    }else if (self.communtyView.height > screenHeight-64-44){
        self.scrollViewLayout.constant = self.communtyView.height;
    }
}
//隐藏键盘的方法
-(void)dismissKeyboard{
    [self.communtyView.titleTextView resignFirstResponder];
    [self.communtyView.contentTextView resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
